package com.ywl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywl.mapper.AdminMapper;
import com.ywl.pojo.Admin;
import com.ywl.pojo.LoginForm;
import com.ywl.pojo.Teacher;
import com.ywl.service.AdminService;
import com.ywl.util.MD5;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;


/**
 * @author DL5O
 * @version 1.0
 */
@Service("adminServiceImpl")//初始化Service层
@Transactional //输入控制
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public Admin login(LoginForm loginForm) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", loginForm.getUsername());//查询名称
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));//注意这里需要把明文转换为密文

        Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public Admin getAdminById(Long userId) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public IPage<Admin> getAdminOpr(Page<Admin> pagePram, String adminName) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(adminName)){
            queryWrapper.like("name",adminName);
        }
        Page<Admin> page = baseMapper.selectPage(pagePram, queryWrapper);

        return page;
    }
}
